package com.andaily.web.context;

import com.andaily.domain.shared.DateUtils;
import com.andaily.infrastructure.support.LogHelper;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import java.util.TimeZone;

/**
 * @author Shengzhao Li
 */
public class AndailyContextLoaderListener extends ContextLoaderListener {

    private static LogHelper log = LogHelper.create(AndailyContextLoaderListener.class);

    @Override
    public void contextInitialized(ServletContextEvent event) {
        //set default TimeZone
        TimeZone.setDefault(DateUtils.DEFAULT_TIMEZONE);
        log.info("Change current context default TimeZone to [" + TimeZone.getDefault() + "]");

        super.contextInitialized(event);
        WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
        BeanProvider.initialize(applicationContext);
    }
}